<%--
  Created by IntelliJ IDEA.
  User: yingzi
  Date: 2017-6-22
  Time: 15:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <jsp:include page="/static/commonviews/header.jsp"/>
</head>
<body>
<div class="container-fluid" style="height: 100%">
    <div style="width: 100%;height: 8%;padding-top: 10px;min-height: 56px">
        <div id="toolbar" class="button-group">
            <button id="btnAdd" class="btn btn-default" onclick="addTask()"><i class="iconfont icon-xinzeng-copy"></i> 新增</button>
            <button id="btnEdit" class="btn btn-default" onclick="editTask()"><i class="iconfont icon-modify"></i> 修改</button>
            <button id="btnDel" class="btn btn-default" onclick="delTask()"><i class="iconfont icon-shanchu"></i> 删除</button>
            <button id="btnPause" class="btn btn-default" onclick="pauseTask()"><i ></i> 暂停</button>
            <button id="btnResure" class="btn btn-default" onclick="resumeTask()"><i ></i> 恢复</button>
            <button id="btnRun" class="btn btn-default" onclick="runTask()"><i ></i> 立即启动</button>
            <button id="btnPrint" class="btn btn-default" onclick="printTask()"><i class="iconfont icon-iconfontdayin"></i> 打印</button>
            <button id="btnExport" class="btn btn-default" onclick="exportTask()"><i class="iconfont icon-export"></i> 导出</button>
            <button id="btnClose" class="btn btn-default" onclick="exitFrame()"><i class="iconfont icon-tuichu"></i> 退出</button>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <table id="dgInfo"></table>
        </div>
    </div>
</div>

<div  id="add-content" style="display: none;margin-top: 10px;">
    <form id="taskForm" name="taskForm" class="form-horizontal">
        <div class="form-group">
            <div class="col-xs-3 control-label">bean名称</div>
            <div class="col-xs-6">
                <input type="text" class="form-control" id="beanName" name="beanName" placeholder="spring bean名称，如：testTask"/>
            </div>
        </div>
        <div class="form-group">
            <div class="col-xs-3 control-label">方法名称</div>
            <div class="col-xs-6">
                <input type="text" class="form-control" id="methodName" name="methodName" placeholder="方法名称"/>
            </div>
        </div>
        <div class="form-group">
            <div class="col-xs-3 control-label">参数</div>
            <div class="col-xs-6">
                <input type="text" class="form-control" id="params" name="params" placeholder="参数"/>
            </div>
        </div>
        <div class="form-group">
            <div class="col-xs-3 control-label">cron表达式</div>
            <div class="col-xs-6">
                <input type="text" class="form-control" id="cronExp" name="cronExp" placeholder="如：0 0 12 * * ?"/>
            </div>
        </div>
        <div class="form-group">
            <div class="col-xs-3 control-label">备注</div>
            <div class="col-xs-6">
                <input type="text" class="form-control" id="remark" name="remark" placeholder="备注"/>
            </div>
        </div>

    </form>
</div>

<jsp:include page="/static/commonviews/footer.jsp"/>

<script type="text/javascript">
    $(function () {
        //初始化表格
        initTable();
    });

    function initTable() {
        //指定布局高度
        var height = window.parent.document.getElementById('iframe_box').offsetHeight;
        //$('body').css('height',height-70);
        $('#dgInfo').bootstrapTable({
            url			:tsUtils.getRootPath()+'/task/taskList',
            method		:'post',
            striped		:true,					//是否显示间隔栏颜色
            pagination	:true,					//是否分页
            sortable	:false,					//是否启用排序
            pageSize	:10,
            pageList	:[5,10,15,20],
            height		:height-60,
            showFooter  :true,
            columns		:[{
                field:'FID',
                title:'任务ID',
                align:'center',
                width:50,halign:"center"
                ,footerFormatter: function (data) {
                    return "合计";
                }
            },{
                field:'FBEANNAME',
                title:'Bean名称',
                align:'center',
                width:80,halign:"center"
                ,footerFormatter: function (data) {
                    return "测试";
                }
            },{
                field:'FMETHODNAME',
                title:'方法名称',
                width:120,halign:"center",falign:"center"
                ,footerFormatter: function (data) {
                    return "名称";
                }
            },{
                field:'FPARAMS',
                title:'参数信息',
                width:100,halign:"center"
            },{
                field:'FCRON',
                title:'Cron表达式',
                width:120,
                align:'center',halign:"center"
            },{
                field:'FREMARK',
                title:'备注',
                width:80,
                align:'left',halign:"center"
            },{
                field:'FSTATUS',
                title:'状态',
                width:80,
                align:'center',halign:"center"
                ,footerFormatter: function (data) {
                    var count = 0;
                    for (var i in data) {
                        count += data[i].FSTATUS;
                    }
                    return count;
                }
            },{
                field:'FCREATEUSER',
                title:'创建人员',
                width:80,
                align:'left',halign:"center"
            },{
                field:'FCREATETIME',
                title:'创建时间',
                width:100,
                align:'center',halign:"center"
            }],
            onClickRow: function(row, $el){
                $('#dgInfo').find('.success').removeClass('success');
                $el.addClass('success');
            },
            queryParams :function (param) {
                
            },
            onLoadError:function (data) {
                tsUtils.handleError(data);
            }
        });
    }

    //添加任务
    function addTask() {
        $('#add-content form :input').attr('value','');
        $('#add-content form')[0].reset();
        layer.open({
            type:1,
            title:'添加任务',
            area:['500px','380px'],
            skin:'layer-window',
            shadeClose:false,
            resize: true,
            scrollbar: false,
            maxmin: true,//出现最大化按钮
            content:$('#add-content'),
            btn:['确定','关闭'],
            yes:function () {
                saveTask('add',0);
            },success: function(layero, index){
               // layer.full(index);//默认加载完成最大化
               // $("input").attr("disabled",true);//禁用所有input
               // $("input").attr("readonly","readonly");//只读所有input
               // $(".layui-layer-btn0").css('display','none');//禁用确定按钮
            }
        })
    }

    function saveTask(opType,iFID) {
        var url="";
        //新增
        if(opType=='add'){
            url=tsUtils.getRootPath()+"/task/addTask";
        }else if(opType=='edit'){//修改
            url=tsUtils.getRootPath()+"/task/updateTask";
        }
        var formData = sysForm.getFormData('taskForm');
        formData.id=iFID;
        $.ajax({
            type:'post',
            url:url,
            data:formData,
            success:function (data) {
                if(sysString.isEmpty(data.msg)){
                    if(opType=='add'){
                        layer.msg('添加成功',{time:1500});
                        sysForm.resetForm('taskForm');
                    }else{
                        layer.msg('修改成功',{time:1500});
                        layer.closeAll();
                    }
                    $('#dgInfo').bootstrapTable('refresh');

                }else{
                    layer.alert(data.msg);
                }
            },
            error:function (data) {
                layer.alert(data.status+":"+data.statusText);
            }
        });
    }
    //编辑任务
    function editTask(){
        var index=$("#dgInfo").find("tr.success").data('index');
        var oRow=$("#dgInfo").bootstrapTable('getData')[index];
        if(oRow==null){
            layer.alert('未选择任何项',{icon:6});
        }else{
            $.ajax({
                type:'post',
                url:tsUtils.getRootPath()+'/task/getTask',
                data:oRow,
                success:function (data) {
                    if(sysString.isEmpty(data.msg)){
                        //设置表单值
                        setFormValue(data.maprow);
                        layer.open({
                            type:1,
                            title:'修改任务',
                            area:['500px','380px'],
                            skin:'layer-window',
                            shadeClose:false,
                            content:$('#add-content'),
                            btn:['修改','取消'],
                            yes:function () {
                                saveTask('edit',oRow.FID);
                            }
                        })
                    }else{
                        layer.alert(data.msg);
                    }
                },error:function (data) {
                    layer.alert(data.status+":"+data.statusText);
                }
            });
        }
    }
    //设置表单元素值
    function setFormValue(data) {
        $("#beanName").val(data.FBEANNAME);
        $("#methodName").val(data.FMETHODNAME);
        $("#params").val(data.FPARAMS);
        $("#cronExp").val(data.FCRON);
        $("#remark").val(data.FREMARK);
    }

    //删除岗位
    function delTask() {
        var index=$("#dgInfo").find("tr.success").data('index');
        var oRow=$("#dgInfo").bootstrapTable('getData')[index];
        if(oRow==null){
            layer.alert('请选择删除项',{icon:6})
        }else{
            layer.alert('确定要删除这'+oRow.FBEANNAME+'项吗？',{
                time:0,
                btn:['确定','取消'],
                yes:function (index) {
                    $.ajax({
                        type:'post',
                        url:tsUtils.getRootPath()+'/task/deleteTask',
                        data:oRow,
                        success:function (data) {
                            if(sysString.isEmpty(data.msg)){
                                $('#dgInfo').bootstrapTable('refresh');
                                layer.close(index);
                                layer.msg('删除成功',{time:1500});
                            }else{
                                layer.close(index);
                                layer.msg('删除失败',{time:1500});
                            }
                        },
                        error:function (data) {
                            layer.alert(data.status+":"+data.statusText);
                        }
                    });
                }
            })
        }

    }

    //打印岗位
    function printTask() {
        tableUtils.init('dgInfo');
        tableUtils.doPrint();
    }

    //导出
    function exportTask() {
        /*tableUtils.init('dgInfo');
        tableUtils.exportTableToExcel(0);*/
        var headArr=["这是一个Hearder"];
        SysExcel.exportExcel("定时任务","dgInfo",headArr,null);
    }

    //退出iframe
    function exitFrame() {
        tsUtils.removeIframe();
    }
</script>
</body>
</html>
